package com.skycode7.servlet;

import com.skycode7.bean.AdminBean;
import com.skycode7.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/adminAdd/jsp")
public class AdminAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String adminTitle = req.getParameter("adminTitle");
        String adminContent = req.getParameter("AdminContent");
        AdminBean adminBean = new AdminBean();//实例化一个新闻对象
        //设置新闻对象属性值
        adminBean.setAdminTitle(adminTitle);
        adminBean.setAdminContent(adminContent);
        adminBean.setAdminTime( (new Date()).toString()  );
        adminBean.setAdminName("admin");
        NewsDao newsDao = new NewsDao();
        int result = newsDao.add(adminBean);
        if (result > 0){
            //补充知识 跳转到网页news.html中
            resp.sendRedirect("news.html");
        }else{
            req.getRequestDispatcher("WEB-INF/pages/adminAdd.jsp").forward(req,resp);
        }

    }
}
